package CaixaEletronico;

import java.sql.*;
import java.util.ArrayList;






import CaixaEletronico.Cliente;

import com.mysql.jdbc.PreparedStatement;

public class ClienteDao{

	  private Connection connection;

	  public ClienteDao() {
	    this.connection = new ConnectionFactory().getConnection();
	  }
	 
	  public boolean insert (Cliente cliente) throws SQLException {
		  String sql = "insert into tb_cliente " +
		             "(NOME,CPF,SENHA,SALARIO)" +
		             " values (?,?,?,?);";
		             
		     try {
		         PreparedStatement stmt = (PreparedStatement) connection.prepareStatement(sql);
		         
		         stmt.setString(1,cliente.getNome());
		         stmt.setString(2,cliente.getCPF());
		         stmt.setString(3,cliente.getSenha());
		         stmt.setDouble(4,cliente.getSalario());

		         
		         stmt.execute();
		         stmt.close();
		         
		         
		     } catch (SQLException e) {
		         throw new RuntimeException(e);
		     }
		     return true;
		 }

	  public void getAll() throws SQLException{
		  	
		
		  PreparedStatement stmt = (PreparedStatement) this.connection.prepareStatement("Select*from tb_cliente;");
		  
		  ResultSet rs = stmt.executeQuery();
			
		 while(rs.next()){
			 
			 System.out.println("Cliente : "+rs.getString("NOME"));
			 System.out.println("ID do Cliente : "+rs.getInt("ID"));
				  
				  System.out.println("\n");
		 }
			  
			rs.close();
			stmt.close();
			
	  }
	  
	public Cliente get(int id) throws SQLException {
		Cliente c = new Cliente();
		
		PreparedStatement stmt = (PreparedStatement) this.connection.prepareStatement("Select*from tb_cliente WHERE ID = "+id);
		  
		  ResultSet rs = stmt.executeQuery();
		  
		  if(rs.next()){
			  c.setCPF(rs.getString("CPF"));
			  c.setSalario(rs.getDouble("SALARIO"));
			  c.setNome(rs.getString("NOME"));
			  c.setSenha(rs.getString("SENHA"));
		  }
		  
		  rs.close();
		  stmt.close();
		
		return c;
	}
	public boolean LoginCliente(Cliente cliente) throws SQLException {
		PreparedStatement stmt = (PreparedStatement) this.connection.prepareStatement("Select ID from tb_cliente WHERE CPF = '"+cliente.getCPF()+"' and SENHA = '"+cliente.getSenha()+"'");
		  
		  ResultSet rs = stmt.executeQuery();
		  
		  if(rs.next()){
			  return true;
		  }
		
		return false;
		
	}
	public int PegarId(Cliente cliente) throws SQLException{
		
		PreparedStatement stmt = (PreparedStatement) this.connection.prepareStatement("Select ID from tb_cliente WHERE CPF = "+cliente.getCPF());
		  
		ResultSet rs = stmt.executeQuery();
		
		rs.next();
			
		return rs.getInt("ID");
	
		
	}
	

	public void disconnect() throws SQLException {
		this.connection.close();	
	}
}

